﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WoChu.DeliverCenter.Entity;
using WoChu.Infrastructure.Data;
using WoChu.Infrastructure.Dapper;
using WoChu.Infrastructure.Collection;

namespace WoChu.DeliverCenter.Repository
{
    /// <summary>
    /// 订单主表
    /// </summary>
    public class DeliOrderRepository : RepositoryBase<DeliOrder, string>
    {
        public DeliOrderRepository() : base(ConnectionsDBKey.DeliveryCenter_MS) { }
        public List<DeliOrder> GetCheckList(DeliOrder entity)
        {
            StringBuilder sql = new StringBuilder();
            StringBuilder whereSql = new StringBuilder();
            sql.Append(@"
                    Select ord.OrderNo,OrderStatus,DeliveryStatus,DeliveryPeriod,PointName,SiteID                       
                    ,BatchNo,BatchSubNo,TicketNo--,det.OrderDetailID,det.OrderNo,det.GoodsNo,det.GoodsQty
                    From Deli_Order ord (nolock) 
                   -- Left join Deli_OrderDetail det (nolock) on ord.OrderNo=det.OrderNo 
                    Where 1=1 
            ");
            if (!string.IsNullOrEmpty(entity.DeliveryPeriod))
                whereSql.Append(" And DeliveryPeriod like @DeliveryPeriod+'%' ");
            if (!string.IsNullOrEmpty(entity.OrderNo))
                whereSql.Append(" And ord.OrderNo=@OrderNo");
            sql.Append(whereSql);
            var list = Query(sql.ToString(), entity);
            return list;
            //var list = new Dictionary<string, DeliOrder>();
            //QueryMulti<DeliOrder, DeliOrderDetail>(sql, entity,
            //    (order, detail) =>
            //    {
            //        DeliOrder ord;
            //        if (!list.TryGetValue(order.OrderNo, out ord))
            //        {
            //            ord = order;
            //            list.Add(order.OrderNo, order);
            //        }
            //        ord.DeliOrderDetailList.Add(detail);
            //        return order;
            //    },
            //    "OrderNo");
            //return list.Values.ToList();
        }
    }
}
